using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Headers;

namespace ERP_SaaS_WinForm.ProductionPlan.Request
{
    /// <summary>
    /// API请求封装类
    /// 统一管理HTTP请求，将原来的http://10.223.8.13:7777替换为http://10.223.8.13:8000/lineconsul
    /// </summary>
    public static class ApiRequestHelper
    {
        private static readonly string BaseUrl = "http://10.223.8.13:8000/lineconsul";
        private static readonly HttpClient httpClient;

        static ApiRequestHelper()
        {
            httpClient = new HttpClient();
            httpClient.Timeout = TimeSpan.FromSeconds(30);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        /// <summary>
        /// 执行GET请求
        /// </summary>
        /// <param name="endpoint">API端点</param>
        /// <returns>响应内容</returns>
        public static async Task<string> GetAsync(string endpoint)
        {
            try
            {
                string url = $"{BaseUrl}{endpoint}";
                var response = await httpClient.GetAsync(url);
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                throw new Exception($"GET请求失败: {endpoint}, 错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 执行POST请求
        /// </summary>
        /// <param name="endpoint">API端点</param>
        /// <param name="jsonContent">JSON内容</param>
        /// <returns>响应内容</returns>
        public static async Task<string> PostAsync(string endpoint, string jsonContent)
        {
            try
            {
                string url = $"{BaseUrl}{endpoint}";
                var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                var response = await httpClient.PostAsync(url, content);
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                throw new Exception($"POST请求失败: {endpoint}, 错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 执行POST请求（使用HttpContent）
        /// </summary>
        /// <param name="endpoint">API端点</param>
        /// <param name="content">HTTP内容</param>
        /// <returns>响应内容</returns>
        public static async Task<string> PostAsync(string endpoint, HttpContent content)
        {
            try
            {
                string url = $"{BaseUrl}{endpoint}";
                var response = await httpClient.PostAsync(url, content);
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                throw new Exception($"POST请求失败: {endpoint}, 错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取完整的API URL
        /// </summary>
        /// <param name="endpoint">API端点</param>
        /// <returns>完整的URL</returns>
        public static string GetFullUrl(string endpoint)
        {
            return $"{BaseUrl}{endpoint}";
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Dispose()
        {
            httpClient?.Dispose();
        }
    }
} 